package com.gfhd.multithread.service;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadDemo3 {
    public void test(){
        MyCallable callable = new MyCallable(100);
        FutureTask<Integer> future = new FutureTask<>(callable);
        new Thread(future).start();

        MyCallable callable2 = new MyCallable(200);
        FutureTask<Integer> future2 = new FutureTask<>(callable2);
        new Thread(future2).start();

        try {
            int rs1 = future.get();
            int rs2 = future2.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class MyCallable implements Callable<Integer>{
    private int n;
    public MyCallable(int n){
        this.n = n;
    }
    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        return sum;
    }
}